﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyFrog : Enemy
{
    // 跳跃的力度
    public float jumpFroce = 3;

    [Tooltip("地面检测的层")]
    public LayerMask ground;

    // Start is called before the first frame update
    protected override void Start()
    {
        base.Start();

        coll = GetComponent<CircleCollider2D>();
    }

    // Update is called once per frame
    void Update()
    {
        // 动画切换
        SwitchAnim();
    }

    // Animation Event调用
    public override void Movement()
    {
        anim.SetBool("jumping", true);

        if (dirction)
        {
            rb.velocity = new Vector2(-speed, jumpFroce);
        }
        else
        {
            rb.velocity = new Vector2(speed, jumpFroce);
        }
    }

    // 朝向变换 Animation Event调用
    void SwitchFaceDirction()
    {
        if (transform.position.x <= APosition.x)
        {
            dirction = false;
            transform.localScale = new Vector3(-1, 1, 1);
        }
        else if (transform.position.x >= BPosition.x)
        {
            dirction = true;
            transform.localScale = new Vector3(1, 1, 1);
        }
    }

    // 动画切换
    void SwitchAnim()
    {
        // 判断掉落
        if (rb.velocity.y < 0.1f)
        {
            anim.SetBool("jumping", false);
            anim.SetBool("falling", true);
        }

        // 地面检查
        if (coll.IsTouchingLayers(ground))
        {
            anim.SetBool("falling", false);
        }
    }
}
